package com.mmdccj.yugu.controller;

import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.PostDto;
import com.mmdccj.yugu.entity.Post;
import com.mmdccj.yugu.service.PostService;
import com.mmdccj.yugu.utils.SnowFlakeUtil;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: star_
 * @date: 2023-11-30 18:47
 */
@Slf4j
@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 发送动态
     *
     * @param session session
     * @param post    动态信息
     * @return Result<String>
     */
    @PostMapping("/create")
    public Result<String> create(HttpSession session, @RequestBody Post post) {
        // 从session中获取用户id
        String userId = (String) session.getAttribute("user");
        log.info("用户 {} 创建动态...", userId);
        if (post.getImageList() != null) {
            log.info("动态的图片: {}", post.getImageList());
        }
        post.setId(SnowFlakeUtil.getSnowFlakeId().toString());
        post.setUserId(userId);
        post.setCreateTime(LocalDateTime.now());
        // 插入数据
        postService.insertPost(post);
        return Result.success("创建成功");
    }


    @GetMapping("/getOne")
    private Result<PostDto> getOne(String postId) {
        log.info("获取单个动态");
        PostDto postDto = postService.getOne(postId);
        if (postDto != null) {
            return Result.success("成功", postDto);
        } else {
            return Result.error("动态不存在");
        }
    }

    /**
     * 分页查询动态
     *
     * @param currentPage 当前页码
     * @param pageSize    每页大小
     * @return Result<PageInfo < PostDto>>
     */
    @GetMapping("/getPost")
    private Result<PageInfo<PostDto>> getPost(int currentPage, int pageSize) {
        log.info("获取动态...");
        PageInfo<PostDto> pageInfo = postService.getPost(currentPage, pageSize);

        return Result.success("成功", pageInfo);
    }

    @GetMapping("/getMyPost")
    private Result<PageInfo<PostDto>> getMyPost(int currentPage, int pageSize, String userId, HttpSession session) {
        log.info("获取自己的动态");
        if (userId == null) {
            userId = (String) session.getAttribute("user");
            if (userId == null) return Result.error("未登录");
        }

        PageInfo<PostDto> pageInfo = postService.getMyPost(currentPage, pageSize, userId);

        return Result.success("成功", pageInfo);
    }

    @GetMapping("/getCommentCount")
    private Result<Integer> getCommentCount(String postId, String type) {
        return Result.success("成功", postService.getCommentCount(postId, type));
    }

    /**
     * 点赞评论
     * Todo
     *
     * @param session session
     * @param postId  id
     * @return Result<String>
     */
    @PostMapping("/like")
    public Result<String> like(HttpSession session, String postId) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return Result.error("未登录");
        }
        if (postId == null) {
            return Result.error("参数为空");
        }
        if (postService.like(userId, postId)) {
            return Result.success("点赞成功");
        } else {
            return Result.error("已经点赞");
        }
    }
}
